// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	dataset_sitc.dta
//			dataset_hs8.dta
//			dataset_tsusa.dta
//			dataset_tsushts.dta
//			emp_sic1987.dta

// outputs:	figureH1 ... figureH6
//			figureH16 ... figureH20

set varabbrev off

global dir_int "../20 Intermediate files/"
global dir_out "../31 Final output files appendix/"
global dir_raw "../01 Raw data/"

// This code creates figures H1--H6 and H16--H20 in the appendix. 

loc h1 1
loc h2 1
loc h3 1
loc h4 1
loc h5 1
loc h6 1
loc h16 1
loc h17 1
loc h18 1
loc h19 1
loc h20 1

timer on 1
////////////////////////////////////////////////////////////////////////////////
// Fig H1
if `h1' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc tstat 1.96
	loc tmax 25
	loc fe="absorb(sitcn#year ctyn#year)"
	loc indicator="ibn.chn#c."
	loc indicator2="i1.chn#c."
	loc y ltariff_jst
	loc x ltariff_jst
	loc treated chn
	loc cluster="cluster sitcn#year"
	loc controls=""
	gen temp1 = phase==0 & fta==0 & (`treated'==1 | ntr==1)
	gen temp2 = temp1 & year==1979
	gen temp3 = temp1 & year>1979
	eststo clear
	xtset id year
	set more off
	forv z = 2/2 {
	gen temp_beta`z' = .
	gen temp_ul`z' = .
	gen temp_ll`z' = .
	forv t=1/`tmax' {
		loc t2=1
		eststo : qui reghdfe F`t'.S`t'.`y'  `indicator'F`t2'.S`t2'.`x' if temp`z'==1, `fe'
		replace temp_beta`z' = _b[`indicator2'F`t2'.S`t2'.`x'] if _n==`t'
		replace temp_ul`z' = _b[`indicator2'F`t2'.S`t2'.`x'] + `tstat'*_se[`indicator2'F`t2'.S`t2'.`x'] if _n==`t'
		replace temp_ll`z' = _b[`indicator2'F`t2'.S`t2'.`x'] - `tstat'*_se[`indicator2'F`t2'.S`t2'.`x'] if _n==`t'
		 }
	}
	gen temp_yr = _n+1979
	replace temp_yr=. if temp_beta2==.

	translator set Graph2pdf fontface "CMU Serif"
	graph set window fontface "CMU Serif"

	loc color black
	twoway line temp_beta2 temp_yr, lcolor(`color') lwidth(*1.5) || ///
	line temp_ul2 temp_yr, lcolor(`color') lwidth(*0.5) || ///
	line temp_ll2 temp_yr, lcolor(`color') lwidth(*0.5) ||, ///
	 legend(off) graphregion(color(white)) bgcolor(white) ///
	  yline(1, lwidth(*0.5) lcolor(black) lpatt(solid)) ///
	  ylabel(0.8(0.1)1.2 , nogrid labsize(3.5) format(%3.1f) angle(horizontal)) ytitle("", size(4)) ///
	 xlabel(1980(5)2005 , nogrid labsize(3.5)) xtitle("", size(4))
	drop temp*
	//graph export "${dir_out}autocorr_tar_chn1980.pdf", as(pdf) replace
	graph export "${dir_out}figureH1.pdf", as(pdf) replace
	
}

////////////////////////////////////////////////////////////////////////////////
// Fig H2
if `h2' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc var gp
	loc threshold 0
	g extensive = log(count`var'_jst)
	g intensive = log(v_jst/count`var'_jst)
	bysort sitc: egen tempcounts=max(count`var'_jst)
	drop if tempcounts<=`threshold'

	loc FE="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	loc cluster="cluster sitcn#ctyn"
	loc x lgap_med
	loc treated chn
	loc controls=""
	loc lastyr 2008
	loc firstyr 1974
	gen temp= fta==0 & (`treated'==1 | ntr==1) & phase==0 
	foreach y in ext int {
	eststo clear
	xtset id year
	eststo : qui reghdfe `y'ensive ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	gen temp_b_`y' =.
	gen temp_ub_`y' =.
	gen temp_lb_`y' =.
	forval t = `firstyr'/`lastyr' {
		qui replace temp_b_`y' = _b[`t'.year#i1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub_`y' = _b[`t'.year#i1.`treated'#c.`x'] + 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb_`y' = _b[`t'.year#i1.`treated'#c.`x'] - 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
	}
	}
	sort year
	egen temptag2 = tag(year)
	loc color1 black
	loc color2 navy
	loc patt1 solid
	loc patt2 dash
	loc lastyr 2008
	loc firstyr 1974
	twoway line temp_b_int year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1') lpatt(`patt1') lwidth(*1.5) ///
		|| line temp_ub_int year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1') lpatt(`patt1') lwidth(*0.5) ///
		|| line temp_lb_int year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1')  lpatt(`patt1') lwidth(*0.5) ///
		|| line temp_b_ext year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1') lpatt(`patt2') lwidth(*1.5) ///
		|| line temp_ub_ext year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1') lpatt(`patt2') lwidth(*0.5) ///
		|| line temp_lb_ext year if temptag2==1 & year>=`firstyr' & year<=`lastyr', lcolor(`color1')  lpatt(`patt2') lwidth(*0.5) ///
		graphregion(color(white)) bgcolor(white) ysize(10) xsize(20)  ///
		legend(ring(0) col(2) pos(6) region(color(white)) symxsize(8.5) order(1 "Intensive Margin" 4 "Extensive Margin")) ///
		yline(0, lcolor(black) lwidth(*0.5) lpatt(solid)) ///
		xline(1980, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		xline(2001, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		ylabel( , nogrid angle(horizontal) format(%3.0f) labsize(3.5)) ytitle("NTR-Gap Elasticity") ///
		xtitle("") xlabel(`firstyr'(2)`lastyr',  nogrid labsize(3.5))
	graph export "${dir_out}figureH2.pdf", as(pdf) replace
}

////////////////////////////////////////////////////////////////////////////////
// Fig H3
if `h3' == 1{
	use "${dir_raw}Colombia/Exporter_2006_2018.dta", clear

	tostring Product_ID, gen(hs10)
	replace hs10="0"+hs10 if length(hs10)==9
	g hs6=substr(hs10,1,6)
	rename Plant_ID plant
	rename CountryISO cty
	rename EX_KG qty
	rename EX_USD val
	drop if cty!="USA"
	keep hs10 plant cty val qty year hs6

	merge m:1 hs6 using "${dir_raw}SITC2-HS02.dta", gen(merge1)
	drop if merge1==2
	rename sitc2 sitc2_orig
	merge m:1 hs6 using "${dir_raw}SITC2-HS07.dta", gen(merge2)
	drop if merge2==2
	replace sitc2_orig=sitc2 if sitc2_orig=="" & year>=2007
	drop sitc2
	merge m:1 hs6 using "${dir_raw}SITC2-HS12.dta", gen(merge3)
	drop if merge3==2
	replace sitc2_orig=sitc2 if sitc2_orig=="" & year>=2012
	drop sitc2
	merge m:1 hs6 using "${dir_raw}SITC2-HS17.dta", gen(merge4)
	drop if merge3==2
	replace sitc2_orig=sitc2 if sitc2_orig=="" & year>=2017
	drop merge* sitc2
	rename sitc2_orig sitc
	drop if sitc=="" | hs10==""

	bysort sitc year: egen val_exports = sum(val)
	bysort sitc year: egen countfp = nvals(plant hs10)
	bysort sitc year: egen countf = nvals(plant)
	egen temptag = tag(sitc year)
	drop if temptag==0 | year>2008
	keep sitc year count* val_exports
	save "${dir_int}temporary_colombia.dta", replace

	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009 | year<2006 | cty!="colombia"
	keep v_jst sitc year count*

	merge m:1 sitc year using "${dir_int}temporary_colombia.dta"
	drop if _merge!=3
	drop _merge

	// Panel A
	loc t 2006
	loc y countf
	loc x countgp_jst
	foreach p in 1 99 {
		foreach var in `x' `y' {
			egen temp_p`p'_`var' = pctile(`var'), p(`p')
		}
	}
	gen temp_extreme = `x'> temp_p99_`x' | `y'> temp_p99_`y'
	g temp = 1 & year==`t' & temp_extreme==0
	qui sum temp_p99_`x'
	loc bound_x = r(mean)

	reg `y' `x' if temp==1
	scalar tempbeta=_b[`x']
	scalar tempse=_se[`x']
	scalar tempr2=e(r2)
	local tempbeta2: di %4.2f scalar(tempbeta)
	local tempse2: di %4.2f scalar(tempse)
	local tempr22: di %4.2f scalar(tempr2)
	scatter `y' `x' if temp==1, msymbol(oh) mcolor(blue)  || ///
	lfit `y' `x' if temp==1, lcolor(blue) || ///
	function y=x, lcolor(black) lpatt(dash) lwidth(*0.75) range(0 `bound_x') ||, ///
	text(85 90 "y=x") ///
	text(115 75 "y = `tempbeta2'x") ///
	graphregion(color(white)) bgcolor(white) legend(off) ///
	ylabel( , nogrid angle(horizontal) labsize(3.5)) ytitle("Count Plants-Products Exports", size(4)) ///
	xlabel( , nogrid format(%3.0f) labsize(3.5)) xtitle("Count Products-Entry Ports Imports", margin(t=3) size(4))
	drop temp*
	graph export "${dir_out}figureH3A.pdf", as(pdf) replace

	
	// Panel B
	loc t 2006
	loc y countfp
	loc x countgp_jst
	foreach p in 1 99 {
		foreach var in `x' `y' {
			egen temp_p`p'_`var' = pctile(`var'), p(`p')
		}
	}
	gen temp_extreme = `x'> temp_p99_`x' | `y'> temp_p99_`y'
	g temp = 1 & year==`t' & temp_extreme==0
	qui sum temp_p99_`x'
	loc bound_x = r(mean)

	reg `y' `x' if temp==1
	scalar tempbeta=_b[`x']
	scalar tempse=_se[`x']
	scalar tempr2=e(r2)
	local tempbeta2: di %4.2f scalar(tempbeta)
	local tempse2: di %4.2f scalar(tempse)
	local tempr22: di %4.2f scalar(tempr2)
	scatter `y' `x' if temp==1, msymbol(oh) mcolor(blue)  || ///
	lfit `y' `x' if temp==1, lcolor(blue) || ///
	function y=x, lcolor(black) lpatt(dash) lwidth(*0.75) range(0 `bound_x') ||, ///
	text(85 90 "y=x") ///
	text(115 75 "y = `tempbeta2'x") ///
	graphregion(color(white)) bgcolor(white) legend(off) ///
	ylabel( , nogrid angle(horizontal) labsize(3.5)) ytitle("Count Plants-Products Exports", size(4)) ///
	xlabel( , nogrid format(%3.0f) labsize(3.5)) xtitle("Count Products-Entry Ports Imports", margin(t=3) size(4))
	drop temp*
	graph export "${dir_out}figureH3B.pdf", as(pdf) replace
}
////////////////////////////////////////////////////////////////////////////////
// Fig H4
if `h4' == 1{
	use "${dir_int}dataset_world.dta", clear
	drop if year>2008 | year<1974

	loc x ls_med
	loc lastyr 2008
	loc firstyr 1974
	g tempyear = year!=`lastyr'
	drop if year>`lastyr'
	loc controls=""
	loc cluster="cluster sitcn#sourcen"
	local FE1 sitcn#year sourcen#sitcn sourcen#year
	local FE2 destn#sitcn#year sourcen#sitcn#year
	local FE3 destn#sitcn#year sourcen#sitcn#year destn#sitcn#sourcen
	gen temp1= phase==0 & source!="HKG" & dest!="HKG" & ntr==1 & dest=="USA"
	gen temp2= phase==0 & source!="HKG" & dest!="HKG" & ntr==1
	gen temp3= phase==0 & source!="HKG" & dest!="HKG" & ntr==1
	eststo clear
	foreach z in 1 2 3 {
		gen temp_b`z' =.
		gen temp_ub`z' =.
		gen temp_lb`z' =.
		eststo : qui reghdfe lv_ijst ibn.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x' i1.usa_i#i1.chn_j#c.`x' if temp`z'==1, absorb(`FE`z'') vce(robust)
		forval t = 1974/2007 {
			qui replace temp_b`z' = _b[`t'.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x'] if year==`t'
			qui replace temp_ub`z' = _b[`t'.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x'] + 1.96*_se[`t'.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x'] if year==`t'
			qui replace temp_lb`z' = _b[`t'.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x'] - 1.96*_se[`t'.year#i1.tempyear#i1.usa_i#i1.chn_j#c.`x'] if year==`t'
		}
	}
	sort year
	egen temptag = tag(year)
	drop if temptag==0
	save "${dir_int}results_annual_gap_world.dta", replace

	translator set Graph2pdf fontface "CMU Serif"
	graph set window fontface "CMU Serif"

	twoway line temp_b1 year if temptag==1, lcolor(black) lwidth(*1.5) ///
		|| line temp_ub1 year if temptag==1, lcolor(black) lwidth(*0.5) ///
		|| line temp_lb1 year if temptag==1, lcolor(black) lwidth(*0.5) ///
		|| line temp_b2 year if temptag==1, lcolor(black) lpatt(dash) lwidth(*1.5) ///
		|| line temp_ub2 year if temptag==1, lcolor(black) lpatt(dash) lwidth(*0.5) ///
		|| line temp_lb2 year if temptag==1, lcolor(black)  lpatt(dash) lwidth(*0.5) ///
		xline(1980, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		xline(2001, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		yline(0, lcolor(black) lwidth(*0.5))  ///
		ysize(10) xsize(20) graphregion(color(white)) bgcolor(white) ///
		legend(col(2) ring(0) pos(6) symxsize(8.5) region(color(white)) order(1 "U.S. only - Baseline" 4 "World Trade Flows")) ///
		xtitle("") xlabel(1974(2)2008, labsize(3.5)) ///
		ylabel(-12(2)2 , labsize(3.5) angle(horizontal) nogrid) ytitle("NTR-Gap Elasticity", size(4)) xlabel(,nogrid)
	graph export "${dir_out}figureH4.pdf", as(pdf) replace
}

////////////////////////////////////////////////////////////////////////////////
// Fig H5
if `h5' == 1{
	// Panel A
	use "${dir_int}dataset_tsusa.dta", clear

	loc FE="absorb(tsusan#year tsusan#ctyn ctyn#year)"
	loc x lnntr_chn
	loc treated chn
	loc controls=""
	loc cluster="cluster tsusan#ctyn"
	gen temp= fta==0 & (`treated'==1 | ntr_cty==1)
	gen temp_b =.
	gen temp_ub =.
	gen temp_lb =.
	eststo clear
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	forval t = 1974/1987 {
		qui replace temp_b = _b[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub = _b[`t'.year#1.`treated'#c.`x'] + 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb = _b[`t'.year#1.`treated'#c.`x'] - 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
	}
	drop if temp_b==.
	egen temptag=tag(year)
	drop if temptag==0
	keep year temp_*
	save "${dir_int}results_annual_gap_tsusa.dta", replace

	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc x lgap_med
	loc treated chn
	loc FE="absorb(ctyn#year sitcn#ctyn sitcn#year)"
	loc cluster="cluster sitcn#ctyn"
	loc controls=""
	gen temp = (phase==0) & (fta!=1) & (ntr==1 | chn==1)
	gen temp_b2 =.
	gen temp_ub2 =.
	gen temp_lb2 =.
	eststo clear
	xtset id year
	g tempyear = year!=1988
	eststo : 
	eststo: reghdfe lv_jst ibn.year#i1.tempyear#i1.`treated'#c.`x' i1.`treated'#c.`x' `controls' if temp==1 & year<1989, `FE' vce(`cluster')
	forval t = 1974/1987 {
		qui replace temp_b2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] + 1.96*_se[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] - 1.96*_se[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
	}
	
	drop if temp_b2==.
	egen temptag=tag(year)
	drop if temptag==0
	keep year temp_*

	merge 1:1 year using "${dir_int}results_annual_gap_tsusa.dta"
	sort year

	translator set Graph2pdf fontface "CMU Serif"
	graph set window fontface "CMU Serif"

	twoway line temp_b year, lcolor(black) lwidth(*1.5) lpatt(dash) ///
		|| line temp_ub year, lcolor(black) lwidth(*0.5) lpatt(dash) ///
		|| line temp_lb year, lcolor(black)  lwidth(*0.5) lpatt(dash) ///
		|| line temp_b2 year, lcolor(black) lwidth(*1.5) ///
		|| line temp_ub2 year, lcolor(black) lwidth(*0.5) ///
		|| line temp_lb2 year, lcolor(black)  lwidth(*0.5) ///
		yline(0, lcolor(black) lwidth(*0.5) lpatt(solid)) ///
		xline(1980, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		text(-1 1978 "NTR Access", color(black)) ///
		ysize(10) xsize(10) graphregion(color(white)) bgcolor(white) ///
		legend(ring(0) pos(5) col(1) order(1 "TS-USA" 4 "SITC") region(color(white)) symxsize(8.5)) ///
		xlabel(1974(2)1988, nogrid labsize(3.5)) xtitle("") ///
		ytitle("NTR-Gap Elasticity", size(4)) ylabel( , nogrid angle(horizontal) labsize(3.5))
	graph export "${dir_out}figureH5a.pdf", as(pdf) replace

	//////////////////
	// Panel B
	clear all 
	use "${dir_int}dataset_hs8.dta", clear
	drop if year>2008 

	loc FE="absorb(hs8n#year hs8n#cty cty#year)"
	loc controls="ltariff_jst"
	loc cluster="cluster hs8n#cty"
	loc x lgap_2001
	loc treated chn
	gen temp= fta==0 & (`treated'==1 | ntr_cty==1)  & phase==0
	gen temp_b =.
	gen temp_ub =.
	gen temp_lb =.
	eststo clear
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	forval t = 1989/2007 {
		qui replace temp_b = _b[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub = _b[`t'.year#1.`treated'#c.`x'] + 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb = _b[`t'.year#1.`treated'#c.`x'] - 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
	}
	drop if temp_b==.
	egen temptag=tag(year)
	drop if temptag==0
	keep year temp_*
	save "${dir_int}results_annual_gap_hs8.dta", replace

	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc x lgap_med
	loc treated chn
	loc FE="absorb(ctyn#year sitcn#ctyn sitcn#year)"
	loc cluster="cluster sitcn#ctyn"
	loc controls=""
// 	gen temp= baseline==1 
	gen temp = (phase==0) & (fta!=1) & (ntr==1 | chn==1)
	gen temp_b2 =.
	gen temp_ub2 =.
	gen temp_lb2 =.
	eststo clear
	xtset id year
	g tempyear = year!=2008
	eststo : qui reghdfe lv_jst ibn.year#i1.tempyear#i1.`treated'#c.`x' i1.`treated'#c.`x' `controls' if temp==1 & year>1988, `FE' vce(`cluster')
	forval t = 1989/2007 {
		qui replace temp_b2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] + 1.96*_se[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb2 = _b[`t'.year#i1.tempyear#1.`treated'#c.`x'] - 1.96*_se[`t'.year#i1.tempyear#1.`treated'#c.`x'] if year==`t'
	}
	drop if temp_b2==.
	egen temptag=tag(year)
	drop if temptag==0
	keep year temp_*
	merge 1:1 year using  "${dir_int}results_annual_gap_hs8.dta"

	translator set Graph2pdf fontface "CMU Serif"
	graph set window fontface "CMU Serif"

	sort year
	twoway line temp_b year, lcolor(black) lwidth(*1.5) lpatt(dash) ///
		|| line temp_ub year, lcolor(black) lwidth(*0.5 lpatt(dash)) ///
		|| line temp_lb year, lcolor(black)  lwidth(*0.5 lpatt(dash)) ///
		|| line temp_b2 year, lcolor(black) lwidth(*1.5) ///
		|| line temp_ub2 year, lcolor(black) lwidth(*0.5) ///
		|| line temp_lb2 year, lcolor(black)  lwidth(*0.5) ///
		yline(0, lcolor(black) lwidth(*0.5)) ///
		xline(2001, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		text(-3.5 2001.5 "PNTR Access", color(black) place(e)) ///
		ysize(10) xsize(10) graphregion(color(white)) bgcolor(white) ///
		legend(ring(0) pos(5) col(1) order(1 "HS-8" 4 "SITC") region(color(white)) symxsize(8) ) ///
		xlabel(1989(2)2008, nogrid labsize(3.5)) xtitle("") ///
		ytitle("NTR-Gap Elasticity", size(4)) ylabel( , nogrid angle(horizontal) labsize(3.5))
	graph export "${dir_out}figureH5B.pdf", as(pdf) replace
}

////////////////////////////////////////////////////////////////////////////////
// Fig H6
if `h6' == 1{
	use "${dir_int}dataset_tsushts.dta", clear
	drop if t>2008 

	loc FE="absorb(gn#t gn#j j#t)"
	loc controls=""
	loc cluster="cluster id"
	loc x lgap2001_med
	loc treated chn
	gen temp= fta==0 & (`treated'==1 | ntr_j==1) & phase_med==0
	gen temp_b =.
	gen temp_ub =.
	gen temp_lb =.
	eststo clear
	eststo : qui reghdfe lv_jgt ibn.t#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	forval t = 1974/2007 {
		qui replace temp_b = _b[`t'.t#1.`treated'#c.`x'] if t==`t'
		qui replace temp_ub = _b[`t'.t#1.`treated'#c.`x'] + 1.96*_se[`t'.t#1.`treated'#c.`x'] if t==`t'
		qui replace temp_lb = _b[`t'.t#1.`treated'#c.`x'] - 1.96*_se[`t'.t#1.`treated'#c.`x'] if t==`t'
	}
	drop if temp_b==.
	egen temptag=tag(t)
	drop if temptag==0
	keep t temp_*
	rename *_* *_*2
	rename t year
	save "${dir_int}results_annual_gap_tsushts.dta", replace
	
	merge 1:1 year using "${dir_int}results_annual_gap_baseline.dta"
	drop _merge
	rename year t

	translator set Graph2pdf fontface "CMU Serif"
	graph set window fontface "CMU Serif"

	sort t
	twoway line temp_b t, lcolor(black) lwidth(*1.5) ///
		|| line temp_ub t, lcolor(black) lwidth(*0.5) ///
		|| line temp_lb t, lcolor(black) lwidth(*0.5) ///
		|| line temp_b2 t, lcolor(black) lwidth(*1.5) lpatt(dash) ///
		|| line temp_ub2 t, lcolor(black) lwidth(*0.5) lpatt(dash) ///
		|| line temp_lb2 t, lcolor(black) lwidth(*0.5) lpatt(dash) ///
		legend(ring(0) pos(6) col(2) region(color(white)) symxsize(8.5) order(1 "Baseline, SITC" 4 "TSUSA-HTS Concordance")) ///
		yline(0, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		xline(1980, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		xline(2001, lcolor(black) lwidth(*0.5) lpatt(dash)) ///
		ysize(10) xsize(20) graphregion(color(white)) bgcolor(white) ///
		xlabel(1974(2)2008, nogrid labsize(3.5)) xtitle("") ///
		ytitle("NTR-Gap Elasticity", size(4)) ylabel( , nogrid angle(horizontal) labsize(3.5))
	graph export "${dir_out}figureH6.pdf", as(pdf) replace	
}

////////////////////////////////////////////////////////////////////////////////
// Figure H.16
if `h16' == 1{
	clear all
	use "${dir_int}emp_sic1987.dta", clear
	// Panel A
	graph box s1999 if tag_g, bar(1, color(black) fcolor(none))  ///
		over(quarvimchnsh9599_abs_g) nooutsides nofill ///
		ylabel(0(0.2)0.8 0.0 "0" , nogrid angle(horizontal) format(%3.1f) labsize(3.5)) ///
		ytitle("NTR Gap") b1title("Quartiles of chinese import share") ///
		graphregion(color(white)) bgcolor(white) note("")
	graph export "${dir_out}figureH16A.pdf", as(pdf) replace	

	// Panel B
	graph box s1999 if tag_g, bar(1, color(black) fcolor(none))  ///
		over(quarvdomsh9599_g) nooutsides nofill ///
		ylabel(0(0.2)0.8 0.0 "0" , nogrid angle(horizontal) format(%3.1f) labsize(3.5)) ///
		ytitle("NTR Gap") b1title("Quartiles of domestic sales share") ///
		graphregion(color(white)) bgcolor(white) note("")	
	graph export "${dir_out}figureH16B.pdf", as(pdf) replace
}

////////////////////////////////////////////////////////////////////////////////
// Figure H.17
if `h17' == 1{
	
	clear all
	use "${dir_int}emp_sic1987.dta", clear
	
	eststo clear
	gen tempabs = labs_gt - lpiship
	gen tempexp = lvex_gt - lpiship
	loc x1 vimchnsh9599_abs_g
	loc x2 vdomsh9599_g
	loc x3 s1999
	loc wgt [aw=emp90_g]
	gen tempexpsh = 1 - `x2'
	gen tempy1 = lemp - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5
	gen tempy2 = lvship - lpiship - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp
	gen tempy3 = lprode - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5
	gen tempy4 = lprodh - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5
	loc firstyr 1958
	loc lastyr 2008
	loc baseyr 2008
	loc chnsh 0.06
	loc domsh 0.91
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	forval t = 1/4 {
		g temp_beta`t'=.
		g temp_beta`t'_se=.
	}
	eststo: qui reghdfe tempy1 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta1=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	qui replace temp_beta1_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	}
	replace temp = temp & _est_est1==1
	eststo: qui reghdfe tempy2 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta2=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	qui replace temp_beta2_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	}
	replace temp = temp & _est_est2==1
	eststo: qui reghdfe tempy3 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta3=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	qui replace temp_beta3_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	}
	replace temp = temp & _est_est3==1
	eststo: qui reghdfe tempy4 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta4=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	qui replace temp_beta4_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`chnsh'*`domsh' if year==`t'
	}
	loc ci 1.96
	forval t = 1/4 {
		g temp_beta`t'_ub = temp_beta`t'+`ci'*temp_beta`t'_se
		g temp_beta`t'_lb = temp_beta`t'-`ci'*temp_beta`t'_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway line temp_beta1 year if temp, lwidth(*2) lcolor(red) ///
		|| line temp_beta2 year if temp, lwidth(*2) lcolor(blue) lpatt(dash) ///
		|| line temp_beta3 year if temp, lwidth(*2) lcolor(orange) lpatt("--.") ///
		|| connected temp_beta4 year if temp, lwidth(*2) lcolor(black) msymbol(Oh) mlcolor(black) msize(1.5) ///
		graphregion(color(white)) bgcolor(white) legend(order(1 "Baseline" 2 "Sales (deflated)" 3 "Production workers" 4 "Production hours") position(6) row(1)) ///
	ylabel( , nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(1) lcolor(black)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig2, replace)
	esttab, replace b(2) se(2) r2 star(* 0.10 ** 0.05 *** 0.01) drop(_cons) label
	drop temp*
	graph export "${dir_out}figureH17A.pdf", as(pdf) name("fig2") replace

	// B
	eststo clear
	loc x s1999
	loc firstyr 1958
	loc lastyr 2008
	loc baseyr 2008
	loc wgt [aw=emp90_g]
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	gen tempyvar = lvship - lpiship
	loc yvar1 lemp
	loc yvar2 tempyvar
	loc yvar3 lprode
	loc yvar4 lprodh
	forval y = 1/4 {
		di "starting `y'"
	eststo: qui reghdfe `yvar`y'' ibn.year#i1.tempbaseyr#c.`x' if temp `wgt', `fe' vce(cluster sic)
	g temp`y'_b=.
	g temp`y'_se=.
	forv t=`firstyr'/`lastyr' {
		qui replace temp`y'_b=_b[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
		qui replace temp`y'_se=_se[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
	}
	loc ci 1.96
	g temp`y'_ub = temp`y'_b+`ci'*temp`y'_se
	g temp`y'_lb = temp`y'_b-`ci'*temp`y'_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway line temp1_b year if temp, lwidth(*2) lcolor(red) ///
		|| line temp2_b year if temp, lwidth(*2) lcolor(blue) lpat("--") ///
		|| line temp3_b year if temp, lwidth(*2) lcolor(orange) lpat("--.") ///
		|| line temp4_b year if temp, lwidth(*2) lcolor(black) lpat("-..") ///
		, graphregion(color(white)) bgcolor(white) legend(order(1 "Employment" 2 "Sales (deflated)" 3 "Prod workers" 4 "Prod hours") position(6) row(1)) ///
	ylabel( , nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(dot) lcolor(black) lwidth(*0.5)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', nogrid labsize(3.5)) yline(0, lpatt(1) lcolor(black)) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig1, replace)
	drop temp*
	graph export "${dir_out}figureH17B.pdf", as(pdf) name("fig1") replace

}

////////////////////////////////////////////////////////////////////////////////
// Figure H.18
if `h18' == 1{
	
	eststo clear
	gen tempemp = lvship - lpiship
	gen tempabs = labs_gt - lpiship
	gen tempexp = lvex_gt - lpiship
	loc x1 vimchnsh9599_abs_g
	loc x2 vdomsh9599_g
	loc x3 s1999
	loc wgt [aw=emp90_g]
	gen tempexpsh = 1 - `x2'
	gen tempy1 = lemp - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5 
	loc firstyr 1958
	loc lastyr 2008
	loc baseyr 2008
	loc chnsh 0.06
	loc domsh 0.91
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	forval t = 1/5 {
		g temp_beta`t'=.
		g temp_beta`t'_se=.
	}
	eststo: qui reghdfe tempy1 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	test i1985.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' - i1983.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' = 0
	sum `x1' if tag_g, det
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta1=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p25)'*`domsh' if year==`t'
	qui replace temp_beta1_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p25)'*`domsh' if year==`t'
	qui replace temp_beta2=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p50)'*`domsh' if year==`t'
	qui replace temp_beta2_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p50)'*`domsh' if year==`t'
	qui replace temp_beta3=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p75)'*`domsh' if year==`t'
	qui replace temp_beta3_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p75)'*`domsh' if year==`t'
	qui replace temp_beta4=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p90)'*`domsh' if year==`t'
	qui replace temp_beta4_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p90)'*`domsh' if year==`t'
	}
	replace temp = temp & _est_est1==1
	eststo: qui reghdfe lemp ibn.year#i1.tempbaseyr#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta5=_b[`t'.year#i1.tempbaseyr#c.`x3'] if year==`t'
	qui replace temp_beta5_se=_se[`t'.year#i1.tempbaseyr#c.`x3'] if year==`t'
	}
	loc ci 1.96
	forval t = 1/5 {
		g temp_beta`t'_ub = temp_beta`t'+`ci'*temp_beta`t'_se
		g temp_beta`t'_lb = temp_beta`t'-`ci'*temp_beta`t'_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway line temp_beta4 year if temp, lwidth(*2) lcolor(red) ///
		|| line temp_beta3 year if temp, lwidth(*2) lcolor(blue) lpatt(dash) ///
		|| line temp_beta2 year if temp, lwidth(*2) lcolor(black) lpatt("--.") ///
		|| connected temp_beta1 year if temp, lwidth(*2) lcolor(green) msymbol(Oh) mlcolor(green) ///
		graphregion(color(white)) bgcolor(white) legend(order(1 "90 pctile" 2 "75 pctile" 3 "50 pctile" 4 "25 pctile") position(6) row(1)) ///
	ylabel( , nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(1) lcolor(black)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig1, replace) title("")
	esttab, replace b(2) se(2) r2 star(* 0.10 ** 0.05 *** 0.01) drop(_cons) label
	drop temp*
	graph export "${dir_out}figureH18A.pdf", as(pdf) replace

	// B
	eststo clear
	gen tempemp = lvship - lpiship
	gen tempabs = labs_gt - lpiship
	gen tempexp = lvex_gt - lpiship
	loc x1 vimchnsh9599_abs_g
	loc x2 vdomsh9599_g
	loc x3 s1999
	loc wgt [aw=emp90_g]
	gen tempexpsh = 1 - `x2'
	gen tempy1 = lemp - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5 
	loc firstyr 1958
	loc lastyr 2008
	loc baseyr 2008
	loc chnsh 0.06
	loc domsh 0.91
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	forval t = 1/5 {
		g temp_beta`t'=.
		g temp_beta`t'_se=.
	}
	eststo: qui reghdfe tempy1 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	sum `x2' if tag_g, det
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta1=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p25)'*`chnsh' if year==`t'
	qui replace temp_beta1_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p25)'*`chnsh' if year==`t'
	qui replace temp_beta2=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p50)'*`chnsh' if year==`t'
	qui replace temp_beta2_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p50)'*`chnsh' if year==`t'
	qui replace temp_beta3=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p75)'*`chnsh' if year==`t'
	qui replace temp_beta3_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p75)'*`chnsh' if year==`t'
	qui replace temp_beta4=_b[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p90)'*`chnsh' if year==`t'
	qui replace temp_beta4_se=_se[`t'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3']*`r(p90)'*`chnsh' if year==`t'
	}
	replace temp = temp & _est_est1==1
	eststo: qui reghdfe lemp ibn.year#i1.tempbaseyr#c.`x3' `wgt' if temp, `fe' vce(cluster sic)
	forv t=`firstyr'/`lastyr' {
	qui replace temp_beta5=_b[`t'.year#i1.tempbaseyr#c.`x3'] if year==`t'
	qui replace temp_beta5_se=_se[`t'.year#i1.tempbaseyr#c.`x3'] if year==`t'
	}
	loc ci 1.96
	forval t = 1/5 {
		g temp_beta`t'_ub = temp_beta`t'+`ci'*temp_beta`t'_se
		g temp_beta`t'_lb = temp_beta`t'-`ci'*temp_beta`t'_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway  line temp_beta4 year if temp, lwidth(*2) lcolor(red) ///
		|| line temp_beta3 year if temp, lwidth(*2) lcolor(blue) lpatt(dash) ///
		|| line temp_beta2 year if temp, lwidth(*2) lcolor(black) lpatt("--.") ///
		|| connected temp_beta1 year if temp, lwidth(*2) lcolor(green) msymbol(Oh) mlcolor(green) ///
		graphregion(color(white)) bgcolor(white) legend(order(1 "90 pctile" 2 "75 pctile" 3 "50 pctile" 4 "25 pctile") position(6) row(1)) ///
	ylabel( , nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(1) lcolor(black)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig2, replace) title("")
	esttab, replace b(2) se(2) r2 star(* 0.10 ** 0.05 *** 0.01) drop(_cons) label
	graph export "${dir_out}figureH18B.pdf", as(pdf) replace
	drop temp*

}

////////////////////////////////////////////////////////////////////////////////
if `h19' == 1{
	clear all
	use "${dir_int}emp_sic1987.dta", clear
	
	// Panel A: Trade and tariffs
	eststo clear
	loc x s1999
	loc firstyr 1974
	loc lastyr 2018
	loc baseyr 2008
	loc wgt [aw=emp90_g]
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	foreach y in tar_wavg {
		di "starting `y'"
	eststo: reghdfe l`y'_gt ibn.year#i1.tempbaseyr#c.`x' if temp `wgt', `fe' vce(cluster sic)
	g temp_l`y'beta=.
	g temp_l`y'beta_se=.
	forv t=`firstyr'/`lastyr' {
		qui replace temp_l`y'beta=_b[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
		qui replace temp_l`y'beta_se=_se[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
	}
	loc ci 1.96
	g temp_l`y'beta_ub = temp_l`y'beta+`ci'*temp_l`y'beta_se
	g temp_l`y'beta_lb = temp_l`y'beta-`ci'*temp_l`y'beta_se
	}
	loc firstyr 1958
	loc lastyr 2018
	replace temp = year>=`firstyr' & year<=`lastyr'
	foreach y in vim vex {
		di "starting `y'"
	eststo: reghdfe l`y'_gt ibn.year#i1.tempbaseyr#c.`x' if temp `wgt', `fe' vce(cluster sic)
	g temp_l`y'beta=.
	g temp_l`y'beta_se=.
	forv t=`firstyr'/`lastyr' {
		qui replace temp_l`y'beta=_b[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
		qui replace temp_l`y'beta_se=_se[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
	}
	loc ci 1.96
	g temp_l`y'beta_ub = temp_l`y'beta+`ci'*temp_l`y'beta_se
	g temp_l`y'beta_lb = temp_l`y'beta-`ci'*temp_l`y'beta_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway line temp_lvimbeta year if temp, lwidth(*2) lcolor(blue) ///
		|| line temp_lvexbeta year if temp, lwidth(*2) lcolor(black) lpatt("--") ///
		|| connected temp_ltar_wavgbeta year if temp, lwidth(*2) msymbol(Dh) lcolor(black) mlcolor(black) msize(1.5) yaxis(2) ///
	, graphregion(color(white)) bgcolor(white) legend(order(1 "Imports" 2 "Exports" 3 "Tariffs (right)") position(6) row(1)) ///
	ylabel(-5(1)3, nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ylabel(-0.5(0.1)0.3, nogrid format(%3.2f) angle(horizontal) labsize(3.5) axis(2)) yscale(range(-0.5 0.3) axis(2)) ytitle("") yline(0, lpatt(dot) lcolor(black) lwidth(*0.5)) ytitle("", axis(2)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig3, replace)
	drop temp*
	graph export "${dir_out}figureH19A.pdf", as(pdf) replace

	//Panel B: Employment and domestic absorption
	eststo clear
	loc x s1999
	loc firstyr 1958
	loc lastyr 2018
	loc baseyr 2008
	loc wgt [aw=emp90_g]
	loc fe absorb(sic year)
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	loc yvar1 lemp
	loc yvar2 labs_gt
	forval y = 1/2 {
		di "starting `y'"
	eststo: reghdfe `yvar`y'' ibn.year#i1.tempbaseyr#c.`x' if temp `wgt', `fe' vce(cluster sic)
	g temp_l`y'beta=.
	g temp_l`y'beta_se=.
	forv t=`firstyr'/`lastyr' {
		qui replace temp_l`y'beta=_b[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
		qui replace temp_l`y'beta_se=_se[`t'.year#i1.tempbaseyr#c.`x'] if year==`t'
	}
	loc ci 1.96
	g temp_l`y'beta_ub = temp_l`y'beta+`ci'*temp_l`y'beta_se
	g temp_l`y'beta_lb = temp_l`y'beta-`ci'*temp_l`y'beta_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year
	twoway line temp_l1beta year if temp, lwidth(*2) lcolor(blue) ///
		|| line temp_l2beta year if temp, lwidth(*2) lcolor(black) lpatt("--") ///
	, graphregion(color(white)) bgcolor(white) legend(order(1 "Employment" 2 "Domestic absorption") position(6) row(1)) ///
	ylabel(, nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(dot) lcolor(black) lwidth(*0.5)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1971, lcolor(red) lwidth(*0.5) lpatt(dash)) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig3, replace)
	drop temp*
	graph export "${dir_out}figureH19B.pdf", as(pdf) replace
}

////////////////////////////////////////////////////////////////////////////////
// Figure H.20
if `h20' == 1{
	clear all
	use "${dir_int}emp_sic1987.dta", clear
		
	eststo clear
	gen tempabs = labs_gt - lpiship
	gen tempexp = lvex_gt - lpiship
	loc x3 s1999
	loc wgt [aw=emp90_g]
	loc firstyr 1974
	loc lastyr 2008
	loc baseyr 2008
	loc fe absorb(sic year)
	loc pd1 9599
	loc pd2 0509
	g tempbaseyr= year!=`baseyr'
	gen temp = year>=`firstyr' & year<=`lastyr'
	gen tempexpsh = .
	gen tempy1 = .
	forval t = 1/2 {
		g temp_beta`t'=.
		g temp_beta`t'_se=.
		loc x1 vimchnsh`pd`t''_abs_g
		loc x2 vdomsh`pd`t''_g
		replace tempexpsh = 1 - `x2'
		replace tempy1 = lemp - c.`x2'#c.tempabs - c.tempexpsh#c.tempexp + ltfp5
		eststo: reghdfe tempy1 ibn.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3'  `wgt' if temp, `fe' vce(cluster sic)
		forv ty=`firstyr'/`lastyr' {
		qui replace temp_beta`t'=-_b[`ty'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3'] if year==`ty'
		qui replace temp_beta`t'_se=_se[`ty'.year#i1.tempbaseyr#c.`x1'#c.`x2'#c.`x3'] if year==`ty'
		}
		loc ci 1.96
		g temp_beta`t'_ub = temp_beta`t'+`ci'*temp_beta`t'_se
		g temp_beta`t'_lb = temp_beta`t'-`ci'*temp_beta`t'_se
	}
	replace temp = tag_t & year>=`firstyr' & year<=`lastyr'
	sort year

	twoway line tradecoeff_base_t year if temp, lwidth(*2) lcolor(black) ///
		|| line tradecoeffub_base_t year if temp, lwidth(*.2) lcolor(black) lpatt("--") ///
		|| line tradecoefflb_base_t year if temp, lwidth(*.2) lcolor(black) lpatt("--") ///
		|| connected tradecoeff_jgNtFE_t year if temp, lwidth(*2) lcolor(green) msymbol(Oh) msize(1.5) mlcolor(green) ///
		|| line tradecoeffub_jgNtFE_t year if temp, lwidth(*.2) lcolor(green) lpatt("--") ///
		|| line tradecoefflb_jgNtFE_t year if temp, lwidth(*.2) lcolor(green) lpatt("--") ///
		|| line temp_beta1 year if temp, lwidth(*2) lcolor(blue) ///
		graphregion(color(white)) bgcolor(white) legend(order(1 "Trade - baseline" 4 "Trade - prodXctry & time FE" 7 "Employment - baseline" ) position(6) row(1)) ///
	ylabel( , nogrid format(%3.1f) angle(horizontal) labsize(3.5)) ytitle("") yline(0, lpatt(1) lcolor(black)) ///
	xtitle("") xlabel(`firstyr'(4)`lastyr', labsize(3.5) nogrid) ///
	xline(1980, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(1990, lcolor(red) lwidth(*0.5) lpatt(dash)) xline(2001, lcolor(red) lwidth(*0.5) lpatt(dash)) name(fig2, replace)
	
	drop temp*
	graph export "${dir_out}figureH20.pdf", as(pdf) replace
}

timer off 1
timer list
